package com.rupeng.service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.xml.ws.RequestWrapper;

import com.rupeng.annotation.RupengCacheable;
import com.rupeng.annotation.RupengClearCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rupeng.pojo.Card;
import com.rupeng.pojo.Chapter;
import com.rupeng.pojo.Segment;
import com.rupeng.util.CommonUtils;

@Service
@RupengCacheable
public class CardService extends BaseService<Card> {
	@Autowired
	private CardSubjectService cardSubjectService;
	@Autowired
    private ChapterService chapterService;
    @Autowired
    private SegmentService segmentService;
    @RupengClearCache
	public void insert(Card card, Long[] subjectIds) {
		super.insert(card);
		if (subjectIds==null || subjectIds.length<1) {
			return;
		}
		Card cardTemp = new Card();
		cardTemp.setName(card.getName());
		card = super.selectOne(cardTemp);
		cardSubjectService.updateFirst(card.getId(), subjectIds);
	}
	@RupengClearCache
	public void update(Card card, Long[] subjectIds) {
		super.update(card);
		if (subjectIds==null || subjectIds.length<1) {
			return;
		}
		cardSubjectService.updateFirst(card.getId(), subjectIds);
	}
    public Map<Chapter, List<Segment>> selectAllCourse(Long id) {
        //先查询出此学习卡所有的Chapter （seqNum asc）
        Chapter param = new Chapter();
        param.setCardId(id);
        List<Chapter> chapterList = chapterService.selectList(param, "seqNum asc");
        if (CommonUtils.isEmpty(chapterList)) {
            return null;
        }
        //再查询出每个Chapter--  List<segment>(seqNum asc)
        Map<Chapter, List<Segment>> courseMap = new LinkedHashMap<Chapter, List<Segment>>();
        for (Chapter chapter : chapterList) {
            Segment segment = new Segment();
            segment.setChapterId(chapter.getId());
            List<Segment> segmentList = segmentService.selectList(segment, "seqNum asc");
            courseMap.put(chapter, segmentList);
        }
        return courseMap;
    }
}
